package net.koodar.forge.system.user.manager;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.koodar.forge.system.user.model.entity.User;
import net.koodar.forge.system.user.dao.UserMapper;
import net.koodar.forge.common.exception.BizException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Domain用户业务接口实现
 *
 * @author liyc
 */
@Service
@RequiredArgsConstructor
public class UserManager extends ServiceImpl<UserMapper, User> {


	public List<User> listByIds(List<Long> userIds) {
		return baseMapper.selectBatchIds(userIds);
	}

	public User findById(Long userId) {
		User user = baseMapper.selectById(userId);
		if (ObjectUtils.isEmpty(user)) {
			throw new BizException(String.format("用户[%s]不存在", userId));
		}
		return user;
	}

	public boolean checkExistsUsername(String username) {
		Long count = baseMapper.selectCount(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
		return count > 0;
	}

	public void disabledUser(User user) {
		user.setStatus(0);
		baseMapper.updateById(user);
	}

	public User findByUsername(String username) {
		List<User> users = baseMapper.selectList(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
		return users.isEmpty() ? null : users.get(0);
	}
}
